using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace WB.Mvc.Forms.Widgets
{
	public class DropDownList : ChoiceWidget
	{
		/// <summary>
		/// Creates an instance of <see cref="DropDownList"/>.
		/// </summary>
		public DropDownList() : this(typeof (DropDownList).Name)
		{
		}

		/// <summary>
		/// Creates an instance of <see cref="DropDownList"/>.
		/// </summary>
		public DropDownList(string name) : this(name, new Dictionary<string, object>())
		{
		}

		/// <summary>
		/// Creates an instance of <see cref="DropDownList"/>.
		/// </summary>
		/// <param name="name"></param>
		/// <param name="attributes"></param>
		public DropDownList(string name, IDictionary<string, object> attributes) : base(name, attributes)
		{
		}

		/// <summary>
		/// Renders the drop down list as XHTML.
		/// </summary>
		public override string ToHtml()
		{
			XElement ele = new XElement("select");

			foreach (string key in Attributes.Keys)
			{
				ele.Add(new XAttribute(key, Attributes[key]));
			}

			foreach (ChoiceOption option in Options)
			{
				var optionElement = new XElement("option", option.Text);
				if (!String.IsNullOrEmpty(option.Value))
				{
					optionElement.Add(new XAttribute("value", option.Value));
				}

				ele.Add(optionElement);
			}

			return ele.ToString();
		}
	}
}